package com.zzt.spider.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzt.spider.enums.SpiderSourceEnum;
import com.zzt.spider.model.entity.SpiderSource;
import com.zzt.spider.model.mapper.SpiderSourceMapper;
import com.zzt.spider.service.spiderService.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class
SpiderService {
    private static final Logger log = LoggerFactory.getLogger(SpiderService.class);
    @Autowired
    private SpiderSourceMapper spiderSourceMapper;
    @Autowired
    private FreekanpianSpiderService freekanpianSpiderService;
    @Autowired
    private FreekanpianGCSpiderService freekanpianGCSpiderService;
    @Autowired
    private FreekanpianZFSpiderService freekanpianZFSpiderService;
    @Autowired
    private FreekanpianZWSpiderService freekanpianZWSpiderService;
    @Autowired
    private FreekanpianOMSpiderService freekanpianOMSpiderService;

    private static final String LOCK_KEY = "spider:lock:";

    public void startSpider() {
        log.info("查询是否要进行爬取");
        SpiderSourceEnum[] spiderSourceEnums = SpiderSourceEnum.values();
        // 生成1-4之间的随机数
        int randomNumber = (int) (Math.random() * spiderSourceEnums.length) ;
        SpiderSource spiderSource = this.spiderSourceMapper.getNeedSpider(spiderSourceEnums[randomNumber].getFrom());
        if (spiderSource == null) {
            log.info("没有，快滚");
        } else {
            log.info(spiderSource.getFrom());
            SpiderSourceEnum spiderSourceEnum = SpiderSourceEnum.getByFrom(spiderSource.getFrom());
            if (spiderSourceEnum == null) {
                log.info("没有配置对应的爬虫种类");
            } else {

                this.spiderSourceMapper.update(new LambdaUpdateWrapper<SpiderSource>().eq(SpiderSource::getId, spiderSource.getId()).set(SpiderSource::getState, 1));
                switch (spiderSourceEnum) {
                    case FREEKANPIAN:
                        log.info("开始爬取【{}】", spiderSource.getFrom());
                        this.freekanpianSpiderService.startSpider(spiderSource);
                        break;
                    case FREEKANPIANGC:
                        log.info("开始爬取【{}】", spiderSource.getFrom());
                        this.freekanpianGCSpiderService.startSpider(spiderSource);
                        break;
                    case FREEKANPIANZF:
                        log.info("开始爬取【{}】", spiderSource.getFrom());
                        this.freekanpianZFSpiderService.startSpider(spiderSource);
                        break;
                    case FREEKANPIANZW:
                        log.info("开始爬取【{}】", spiderSource.getFrom());
                        this.freekanpianZWSpiderService.startSpider(spiderSource);
                        break;
                    case FREEKANPIANOM:
                        log.info("开始爬取【{}】", spiderSource.getFrom());
                        this.freekanpianOMSpiderService.startSpider(spiderSource);
                        break;
                    default:
                        log.info("没有配置对应的爬虫程序");
                }

            }
        }
    }
}
